﻿@{
    ViewData["Title"] = "修改密码";
    Layout = "~/Views/Shared/_ManageLayout.cshtml";
}
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">原密码:</label>
        <div class="layui-input-inline">
            <input type="password" name="oldPassword" 
                   required lay-verify="required" placeholder="请输入原密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新密码:</label>
        <div class="layui-input-inline">
            <input type="password" name="newPassword" 
                   required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">确认新密码:</label>
        <div class="layui-input-inline">
            <input type="password" name="password" 
                   required lay-verify="required|confirmPsw"  placeholder="请再次输入新密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    //Demo
    layui.use(['form','layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        form.verify({
            confirmPsw: function (value, item) { //value：表单的值、item：表单的DOM对象
                var newPassword = $("[name=newPassword]").val();
                if (newPassword != value) {
                    return "两次密码输入不一致";
                }
            }
        }); 
        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.load(2);
            $.ajax({
                url: '/api/admin/updatePassword',
                type: 'put',
                data: data.field,
                dataType: "json",
                async: true,
                complete: function (obj) {
                    layer.closeAll('loading');
                },
                success: function (res) {
                    if (res.success) {
                        layer.msg(res.message, { icon: 1 }, function () {
                            location.replace('/manage/login');
                        });
                    } else {
                        layer.msg(res.message, { icon: 2 });
                    }
                }
            });
            return false;
        });
    });
</script>

